/*
 * @lc app=leetcode.cn id=面试题 17.14 lang=cpp
 * @lcpr version=30204
 *
 * [面试题 17.14] 最小K个数
 */

// @lcpr-template-start
using namespace std;
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// @lcpr-template-end
// @lc code=start
class Solution
{
public:
    vector<int> smallestK(vector<int> &arr, int k)
    {
        vector<int> res(k);
        if (k == 0)
            return res;
        int n = arr.size();
        priority_queue<int> q;
        for (int i = 0; i < k; ++i)
            q.push(arr[i]);
        for (int i = k; i < n; ++i)
        {
            if (q.top() > arr[i])
            {
                q.pop();
                q.push(arr[i]);
            }
        }
        for (int i = 0; i < k; ++i)
        {
            res[i] = q.top();
            q.pop();
        }
        return res;
    }
};
// @lc code=end

/*
// @lcpr case=start
// [1,3,5,7,2,4,6,8]\n4\n
// @lcpr case=end

 */
